<?php

class faculty_IndexController extends Zend_Controller_Action
{
    public function init()
    {
    	defined('BASE_URL')	|| define('BASE_URL', Zend_Controller_Front::getInstance()->getBaseUrl());
    }

    public function indexAction()
    {
    	if($this->getRequest()->isPost()){
    		$data = $this->getRequest()->getPost();
    		if(isset($data['status'])){
    			$ids = $data['status'];
    			$db->updateUnStatus($ids);
    			$this->_redirect("/users/");   			
    		}elseif (isset($data['unstatus'])){
    			$ids = $data['unstatus'];
    			$db->updateStatus($ids);
    			$this->_redirect("/users/");
    		}   		
    		$id = $data['checkBox']; 
    		if(isset($data['update'])&& $id !=""){   			
    			$this->_redirect("/users/index/edit/id/".$id);
    			$row = $db->getUserType($id);
    			$frmuser = new users_Form_FrmUserType();
    			$this->view->frmusers = $frmuser->frmTypeUser($row);
    		}elseif(isset($data['delete'])&& $id !=""){
		    	$db->deleteUser($id);
		    	Application_Form_FrmMessage::message("áž€áž¶áž›áž»áž”áž”áŸ’ážšáž—áŸ�áž‘áž¢áŸ’áž“áž€áž”áŸ’ážšáž¾áž”áŸ’ážšáž¶ážŸáŸ‹áž”áž¶áž“áž‡áŸ„áž‚áž‡áŸ�áž™");
		    	Application_Form_FrmMessage::redirectUrl('/users/');
    		}else { $this->_redirect("/users/");}
    	}
    	$db = new faculty_Model_DbTable_DbFaculty();
    	$datas = $db->getAllFaculty();
    	$this->view->getdata = $datas;
	}
	public function addFacultyAction(){
		try{
			$session_user=new Zend_Session_Namespace('auth');
			$user_type_id = $session_user->level;
			$db = new faculty_Model_DbTable_DbFaculty();
			if($this->getRequest()->isPost()){
				$data = $this->getRequest()->getPost();
				$db->addFaculty($data);
				if(isset($data['save'])){
					Application_Form_FrmMessage::message("បញ្ចូលជោគជ័យ!");
				}
				if(isset($data['save_close'])){
					Application_Form_FrmMessage::message("បញ្ចូលជោគជ័យ");
					Application_Form_FrmMessage::redirectUrl('/faculty/index');
				}
			}
		}catch (Exception $e){
			echo $e->getMessage();
		}
		$form = new faculty_Form_FrmFaculty();
		$this->view->frmFaculty = $form->frmFaculty();
		$this->view->user_type_id = $user_type_id;
	}
	public function editFacultyAction()
	{
		$id = $this->getRequest()->getParam('id');
		$session_user=new Zend_Session_Namespace('auth');
		$user_type_id = $session_user->level;
		if($this->getRequest()->isPost()){
			$data = $this->getRequest()->getPost();
			$update = new faculty_Model_DbTable_DbFaculty();
			$update->editFuculty($data);
			if(isset($data['save'])){
				Application_Form_FrmMessage::message("កែប្រែជោគជ័យ");
				Application_Form_FrmMessage::redirectUrl('/faculty/index/edit-faculty/id/'.$id);
			}
			if(isset($data['save_add'])){
				Application_Form_FrmMessage::message("កែប្រែជោគជ័យ");
				Application_Form_FrmMessage::redirectUrl('/faculty/index/add-faculty');
			}
			if(isset($data['save_close'])){
				Application_Form_FrmMessage::message("កែប្រែជោគជ័យ");
				Application_Form_FrmMessage::redirectUrl('/faculty/index');
			}
		}
		$db = new faculty_Model_DbTable_DbFaculty();
		$row = $db->getFacultyById($id);
		$form = new faculty_Form_FrmFaculty();
		$this->view->frmFaculty = $form->frmFaculty($row);
		$this->view->user_type_id = $user_type_id;
	}		 
	public function deleteFacultyAction(){
		$id = $this->getRequest()->getParam('id');
		$db = new faculty_Model_DbTable_DbFaculty();
		$db->deleteFaculty($id);
		Application_Form_FrmMessage::message("ការលុបទទួលជោគជ័យ!");
		$this->_redirect('/faculty/index');
	}
}

